<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>获取天气</title>
    <style>
      .city {
        width: 200px;
        text-align: center;
        margin: 20px auto;
        border-radius: 5px;
        padding: 5px;
        font-size: 26px;
      }
      table {
        margin: 10px auto;
        width: 800px;
        text-align: center;
        font-size: 20px;
        cursor: pointer;
      }
      th {
        background-color: aquamarine;
      }
      tr:nth-child(2n) {
        background-color: rgb(222, 218, 218);
      }
      th,
      td {
        padding: 10px;
      }
      tr:hover {
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div class="weather"></div>
    <table class="weatherList" border="1px" style="border-collapse: collapse">
      <thead>
        <th>日</th>
        <th>日期</th>
        <th>星期</th>
        <th>天气</th>
        <th>最低温度</th>
        <th>最高温度</th>
      </thead>
      <tbody></tbody>
    </table>
  </body>
  <script src="../jquery-3.3.1.min.js"></script>
  <script>
    // 免费天气接口：http://doc.tianqiapi.com/603579
    $.ajax({
      url: "http://www.tianqiapi.com/api",
      dataType: "jsonp",
      // url: "../data/weather.json", //本地文件
      data: {
        version: "v9",
        appid: "23035354",
        appsecret: "8YvlPNrz",
        city: "南阳",
      },
    }).then(function (res) {
      console.log(res);
      $(".weather").append("<div class='city'>城市：" + res.city + "</div>");
      for (var i in res.data) {
        $(".weatherList").append(
          "<tr><td>" +
            res.data[i].day +
            "</td>" +
            "<td>" +
            res.data[i].date +
            "</td>" +
            "<td>" +
            res.data[i].week +
            "</td>" +
            "<td>" +
            res.data[i].wea +
            "</td>" +
            "<td>" +
            res.data[i].tem2 +
            "</td>" +
            "<td>" +
            res.data[i].tem1 +
            "</td></tr>"
        );
      }
    });
  </script>
</html>
